package com.lst.quartz.config;

import com.lst.quartz.job.FirstJob;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.PropertiesFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.io.IOException;
import java.util.Properties;

/**
 * @author shengtao
 * @Description:
 * @date 2018/12/12 11:53
 */

@Configuration
//@ImportResource(locations = {"classpath:spring-job.xml"})
public class SchedulerConfig {

    @Resource(name = "quartzDateSource")
    private DataSource dataSource;

    @Autowired
    private JobFactory jobFactory;

    @Autowired
    public SchedulerFactoryBean schedulerFactoryBean(Trigger... triggers) throws IOException {
        SchedulerFactoryBean bean = new SchedulerFactoryBean();

        Properties p = new Properties();
        try {
            p.load(this.getClass().getClassLoader().getResourceAsStream("quartz.properties"));
        } catch (IOException e) {
            throw new Error(e);
        }
        bean.setQuartzProperties(p);

        /* 使用应用的数据源 */
        bean.setDataSource(dataSource);

        // 覆盖已存在的任务
        bean.setOverwriteExistingJobs(true);
        // 延时启动定时任务，避免系统未完全启动却开始执行定时任务的情况
        bean.setStartupDelay(15);
        // 注册触发器
        bean.setTriggers(triggers);
        return bean;
    }

//    @Bean
//    public Scheduler scheduler() throws IOException {
//        Scheduler scheduler = schedulerFactoryBean().getScheduler();
//        return scheduler;
//    }
    // 使用jobDetail包装job
    @Bean("firstJob")
    public JobDetail firstJobDetail(){
        return JobBuilder.newJob(FirstJob.class).withIdentity("first").storeDurably().build();
    }
//
    @Bean
    public JobDetail secondJobDetail(){
        return JobBuilder.newJob(FirstJob.class).withIdentity("second").storeDurably().build();
    }

    // 把jobDetail注册到trigger上去
//    @Bean
//    public Trigger myFirstTrigger(){
//        SimpleScheduleBuilder scheduleBuilder = SimpleScheduleBuilder.simpleSchedule()
//                .withIntervalInSeconds(30000).repeatForever();
//        Trigger trigger = TriggerBuilder.newTrigger()
//                .forJob(firstJobDetail())
//                .withIdentity("myFirstTrigger")
//                .withSchedule(scheduleBuilder)
//                .build();
//        return trigger;
//    }

    @Bean
    public Trigger cronTrigger(){
        CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0/10 * * * * ?");

        Trigger trigger = TriggerBuilder.newTrigger()
                .forJob(secondJobDetail())
                .withIdentity("cronTrigger")
                .withSchedule(cronScheduleBuilder)
                .startNow()
                .build();
        return trigger;
    }



}
